﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChatData;
using System.IO;

namespace ChatServerOptions
{
    public partial class Options : Form
    {
        UserStore<StoreRecord> thestore = new UserStore<StoreRecord>(); 
        public Options()
        {
            InitializeComponent();
            Setup();
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Chatserver");
            path = Path.Combine(path,"settings.xml");
            Console.WriteLine("Reading users File");
            thestore = XML.Read(path);
            foreach (KeyValuePair<String, StoreRecord> item in thestore)
            {
                this.kryptonListBox2.Items.Add(item.Value.Username);
            }
        }
        public Options(UserStore<StoreRecord> e)
        {
            InitializeComponent();
            thestore = e;
            foreach (KeyValuePair<String,StoreRecord> item in thestore)
            {
                this.kryptonListBox2.Items.Add(item.Value.Username);
            }
        }
        private void Delete()
        {
            thestore.Remove(this.kryptonListBox2.SelectedItem.ToString());
            this.kryptonListBox2.Items.RemoveAt(this.kryptonListBox2.SelectedIndex);
        }
        private void Setup()
        {
            if (ChatServerOptions.Properties.Settings.Default.Tempfiles != "")
            {
                this.kryptonTempFiles.Text = ChatServerOptions.Properties.Settings.Default.Tempfiles;
            }
            else
            {
                this.kryptonTempFiles.Text = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Chatserver");
            }
        }
        private void edit()
        {
            string user= kryptonListBox2.SelectedItem.ToString();
            string pass= thestore.getPassword(user);
            using (EditUser dialog = new EditUser(user,pass))
            {
                System.Windows.Forms.DialogResult result = dialog.ShowDialog();
                if (result == DialogResult.Yes)
                {
                    StoreRecord temp = new StoreRecord();
                    temp.Username = dialog.Username;
                    temp.Password = Utils.EncodePasswordToBase64(dialog.Password);
                    temp.Ip = "";
                    temp.Port = 8080;
                    temp.Status = "";
                    thestore.Update(temp);
                }
            }
        }
        private void add()
        {
            using (AddUser dialog = new AddUser())
            {
                DialogResult result = dialog.ShowDialog();
                if (result == DialogResult.Yes)
                {
                    StoreRecord temp = new StoreRecord();
                    temp.Username= dialog.Username;
                    temp.Password = Utils.EncodePasswordToBase64(dialog.Password);
                    temp.Ip = "";
                    temp.Port = 8080;
                    temp.Status = "";
                    thestore.Add(temp);
                    this.kryptonListBox2.Items.Add(temp.Username);
                }
            }
        }
        private void Save()
        {
            ChatServerOptions.Properties.Settings.Default.Tempfiles = this.kryptonTempFiles.Text;
            ChatServerOptions.Properties.Settings.Default.Save();
            string path =Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"Chatserver");
            path = Path.Combine(path,"settings.xml");
            Console.WriteLine("Saving Changes");
            XML.Write(thestore, path);
        }
        public string Selection
        {
            get { return kryptonListBox2.SelectedItem.ToString(); }
        }
        //cancel
        private void kryptonButton2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        private void kryptonButtonSave_Click(object sender, EventArgs e)
        {
            Save();
            this.DialogResult = DialogResult.Yes;
            this.Close();
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void buttonSpecHeaderGroup2_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void buttonSpecHeaderGroup1_Click(object sender, EventArgs e)
        {
            add();
        }

        private void kryptonListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.kryptonLabel2.Text = this.kryptonListBox2.SelectedItem.ToString();
                this.kryptonLabel1.Text = thestore.getStatus(this.kryptonListBox2.SelectedItem.ToString());
            }
            catch
            {

            }
        }

        private void kryptonButton3_Click(object sender, EventArgs e)
        {
            edit();
        }
    }
}
